﻿using System;
using System.Collections.Generic;
using Service.Core.Interface;

namespace Service.Core.Result
{
    /// <summary>
    ///     服务层处理结果
    /// </summary>
    public abstract class ServiceResult : IServiceResult
    {
        protected const string KeyIsNotFound = "系统返回的信息，并未在程序中定义";

        protected static readonly Dictionary<ResultType, string> CodeMagsDict = new Dictionary<ResultType, string>
        {
            {ResultType.Error, "太多人了，系统休息一下"},
            {ResultType.Success, "处理成功"},
            {ResultType.Fail, "处理失败"},
            {ResultType.OperateAddIsSuccess, "数据新增成功"},
            {ResultType.OperateDeleteIsSuccess, "数据删除成功"},
            {ResultType.OperateQueryIsSuccess, "数据查询成功"},
            {ResultType.OperateUpdateIsSuccess, "数据更新成功"},
            {ResultType.AccountLoginSuccess, "登录成功"},
            {ResultType.AccountLoginFaile, "登录失败"},
            {ResultType.ParamIsError, "参数不正确"},
            {ResultType.ParamIsNull, "参数为空"},
            {ResultType.AccountIsNotNull, "帐号已经存在"}
        };

        protected ResultType Code;
        protected object Data;
        protected string Msg;
        public abstract object GetCode();
        public abstract void SetCode(object lgCode);
        public abstract object GetData();
        public abstract void SetData(object lgData);
        public abstract void SetMsg(string msg);

        /// <summary>
        ///     获取处理结果表现信息
        /// </summary>
        /// <returns>表现信息</returns>
        public virtual string GetMsg()
        {
            Msg = String.IsNullOrEmpty(Msg)
                ? CodeMagsDict.ContainsKey(Code) ? CodeMagsDict[Code] : Code.ToString("G")
                : Msg;
            return Msg;
        }

        /// <summary>
        ///     验证预期结果
        /// </summary>
        /// <param name="targetResult">预期结果</param>
        /// <returns>ture:一致 false:不一致</returns>
        public virtual bool BoolResult(ResultType targetResult)
        {
            return Code == targetResult;
        }
    }
}